//
//  RegisterViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/13.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *userNameTF;
@property (nonatomic, strong) UITextField *mobileTF;
@property (nonatomic, strong) UITextField *pwdTF;
@property (nonatomic, strong) UITextField *repwdTF;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    
    [self setStyle];
}

- (void) setStyle {
    self.navigationItem.title = @"注册";
    self.view.backgroundColor = GRAY_COLOR;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(reqRegAction)];
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    UILabel *nameLB = [UILabel newAutoLayoutView];
    [self.view addSubview:nameLB];
    
    [nameLB autoPinToTopLayoutGuideOfViewController:self withInset:20];
    [nameLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [nameLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    nameLB.backgroundColor = WHITE_COLOR;
    nameLB.textAlignment = NSTextAlignmentLeft;
    nameLB.text = TEXT_USERNAME;
    
    self.userNameTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_userNameTF];
    
    [_userNameTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:nameLB];
    [_userNameTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:nameLB withOffset:80];
    [_userNameTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _userNameTF.placeholder = @"字母或数字组成";
    _userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userNameTF.keyboardType = UIKeyboardTypeNamePhonePad;
    _userNameTF.returnKeyType = UIReturnKeyDone;
    _userNameTF.delegate = self;
    
    UILabel *lineLB = [UILabel newAutoLayoutView];
    [self.view addSubview:lineLB];
    
    [lineLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:nameLB];
    [lineLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:nameLB];
    [lineLB autoSetDimensionsToSize:CGSizeMake(ScreenHeight, 0.5)];
    lineLB.backgroundColor = GRAY_COLOR;
    
    UILabel *mobileLB = [UILabel newAutoLayoutView];
    [self.view addSubview:mobileLB];
    
    [mobileLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lineLB];
    [mobileLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lineLB];
    [mobileLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    mobileLB.backgroundColor = WHITE_COLOR;
    mobileLB.textAlignment = NSTextAlignmentLeft;
    mobileLB.text = TEXT_MOBILE;
    
    self.mobileTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_mobileTF];
    
    [_mobileTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:mobileLB];
    [_mobileTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:mobileLB withOffset:80];
    [_mobileTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _mobileTF.placeholder = @"11位手机号";
    _mobileTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _mobileTF.keyboardType = UIKeyboardTypeNumberPad;
    _mobileTF.delegate = self;
    
    UILabel *line2LB = [UILabel newAutoLayoutView];
    [self.view addSubview:line2LB];
    
    [line2LB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:mobileLB];
    [line2LB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:mobileLB];
    [line2LB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 0.5)];
    line2LB.backgroundColor = GRAY_COLOR;
    
    UILabel *pwdLB = [UILabel newAutoLayoutView];
    [self.view addSubview:pwdLB];
    
    [pwdLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:line2LB];
    [pwdLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:line2LB];
    [pwdLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    pwdLB.backgroundColor = WHITE_COLOR;
    pwdLB.textAlignment = NSTextAlignmentLeft;
    pwdLB.text = TEXT_PWD;
    
    self.pwdTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_pwdTF];
    
    [_pwdTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:pwdLB];
    [_pwdTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:pwdLB withOffset:80];
    [_pwdTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _pwdTF.placeholder = @"请输入密码";
    _pwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_pwdTF setSecureTextEntry:YES];
    _pwdTF.delegate = self;
    
    UILabel *line3LB = [UILabel newAutoLayoutView];
    [self.view addSubview:line3LB];
    
    [line3LB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:pwdLB];
    [line3LB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:pwdLB];
    [line3LB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 0.5)];
    line3LB.backgroundColor = GRAY_COLOR;
    
    UILabel *repwdLB = [UILabel newAutoLayoutView];
    [self.view addSubview:repwdLB];
    
    [repwdLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:line3LB];
    [repwdLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:line3LB];
    [repwdLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    repwdLB.backgroundColor = WHITE_COLOR;
    repwdLB.textAlignment = NSTextAlignmentLeft;
    repwdLB.text = TEXT_CONFIRM_PWD;
    
    self.repwdTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_repwdTF];
    
    [_repwdTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:repwdLB];
    [_repwdTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:repwdLB withOffset:80];
    [_repwdTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _repwdTF.placeholder = @"请再次输入密码";
    _repwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_repwdTF setSecureTextEntry:YES];
    _repwdTF.delegate = self;
    
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == _userNameTF) {
        [_userNameTF resignFirstResponder];
    }else if(textField == _mobileTF){
        [_mobileTF resignFirstResponder];
    }else if(textField == _pwdTF){
        [_pwdTF resignFirstResponder];
    }else if(textField == _repwdTF) {
        [_repwdTF resignFirstResponder];
    }
    
    return YES;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_userNameTF resignFirstResponder];
    [_mobileTF resignFirstResponder];
    [_pwdTF resignFirstResponder];
    [_repwdTF resignFirstResponder];
}

- (void) reqRegAction {
    
    if (_userNameTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_NICKNAME maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if (_mobileTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_MOBILE maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if (_pwdTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_PWD maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if (_repwdTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_REPWD maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_userNameTF.text forKey:@"uname"];
    [parameters setObject:_mobileTF.text forKey:@"mobile"];
    [parameters setObject:_pwdTF.text forKey:@"pwd"];
    [parameters setObject:_repwdTF.text forKey:@"repwd"];
    
    [[HttpManager instance] requestWithMethod:@"User/register"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          NSDictionary *userInfoTmp = [result objectForKey:@"data"];
                                          NSString *ssidTmp = [userInfoTmp objectForKey:@"user_id"];
                                          NSString *alertText = [userInfoTmp objectForKey:@"info"];
                                          
                                          if (![ssidTmp isEqualToString:@""]) {
                                              [[TMCache sharedCache] setObject:ssidTmp forKey:@"SSID"];
                                          }
                                          
                                          [[TMCache sharedCache] setObject:@"1" forKey:@"loginStatus"];
                                          [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];
                                          
                                          [SVProgressHUD showSuccessWithStatus:alertText maskType:SVProgressHUDMaskTypeBlack];
                                          
                                          [self dismiss];
                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void) dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
